package com.chance.cc.crawler.prod.command.job.domain.news.bilibili.adhoc;

import com.alibaba.fastjson.JSON;
import com.chance.cc.crawler.core.CrawlerEnum;
import com.chance.cc.crawler.core.CrawlerJob;
import com.chance.cc.crawler.core.downloader.HttpConfig;
import com.chance.cc.crawler.core.downloader.HttpPage;
import com.chance.cc.crawler.core.filter.FilterUtils;
import com.chance.cc.crawler.core.record.CrawlerRequestRecord;
import com.chance.cc.crawler.meta.core.bean.CrawlerMetaConstant;
import com.chance.cc.crawler.meta.core.bean.job.CrawlerScheduleJob;
import com.chance.cc.crawler.prod.command.job.domain.news.bilibili.BiliBiliCommonScript;
import com.chance.cc.crawler.prod.command.job.domain.vm.pcauto.PCAutoCommonCrawlerSchedulerJob;
import org.apache.commons.lang3.StringUtils;

import static com.chance.cc.crawler.development.scripts.allfeild.AICCommonField.Tag_Site_Info;

/**
 * @Author: ZhaoHhuan on 2021/9/3 10:58
 * @Email: 18638575967@163.com
 * @Description:
 *      B站文章临时采集
**/
public class BilibiliArticleSimpleCrawlerSchedulerJob extends BiliBiliCommonScript {
    private static final String crawler_level = "simple";

    public static void main(String[] args) {
//        System.out.println(bitAutoScheduler().generateCrawlerTriggerKey());
        publishPcAutoCrawlerSchedulerJobInfo();
    }

    public static CrawlerJob publishPcAutoCrawlerSchedulerJobInfo(){
        CrawlerJob bitAutoScheduler = bitAutoScheduler();

        //发布定时任务
        CrawlerScheduleJob crawlerScheduleJob = new CrawlerScheduleJob();
        crawlerScheduleJob.setDomain(domain);
        crawlerScheduleJob.setCrawlerJob(JSON.toJSONString(bitAutoScheduler));
        crawlerScheduleJob.setJobType(CrawlerMetaConstant.ScheduleCrawlerJobType.crawler.enumVal());
        crawlerScheduleJob.setNote("b站文章临时采集");
        crawlerScheduleJob.setCrawlerKey(bitAutoScheduler.generateCrawlerKey());
        HttpPage httpPage = metaServiceCommand.addOrUpdateCrawlerScheduleJob(crawlerScheduleJob);
        System.out.println("发布任务：" + httpPage.getRawText());
        return bitAutoScheduler;
    }

    public static CrawlerJob bitAutoScheduler(){
        String site = "article";
        String siteBiz = "news";
        String siteInfo = "bmw-20210803";
        String redisRequestQueue = "crawler-bilibili-article-simple-queue";

        CrawlerRequestRecord requestRecord = CrawlerRequestRecord.builder()
                .startPageRequest(domain, CrawlerEnum.CrawlerRequestType.turnPage)
                .domain(domain)
                .recordKey("https://www.bilibili.com/")
                .httpUrl("https://www.bilibili.com/")
                .releaseTime(System.currentTimeMillis())
                .httpConfig(HttpConfig.me(domain))
                .filter(CrawlerEnum.CrawlerRecordFilter.dateRange)
                .addFilterInfo(FilterUtils.redisFilterKeyInfo(domain))
                .addFilterInfo(FilterUtils.dateRangeFilterInfo(24*7,null))
                .proxy(proxy)
                .needParsed(false)
                .needWashed(false)
                .build();
        requestRecord.setSkipPipeline(true);
        requestRecord.setDownload(false);
        requestRecord.tagsCreator().bizTags().addDomain(domain);
        requestRecord.tagsCreator().bizTags().addSite(site);
        requestRecord.tagsCreator().bizTags().addSiteBiz(siteBiz);
        requestRecord.tagsCreator().bizTags().addCustomKV(Tag_Site_Info,siteInfo);


        return CrawlerJob.builder()
                .triggerInfo(domain,
                        CrawlerMetaConstant.ScheduleJobTrigger_Cron,
                        System.currentTimeMillis(),
                        StringUtils.joinWith("-",site, crawler_level,CrawlerMetaConstant.ScheduleJobTriggerJob_Realtime))
                .crawlerRequestQueue(CrawlerMetaConstant.redisRequestQueue(redisRequestQueue))
                .kafkaResultPipeline(null,kafkaTopicForTraceJob,null)
                .fileResultPipeline(null,"/data/chance_crawler_runner/logs/node/bilibili/article-simple-20210903",false)
                .crawlerJobThreadNumber(10)
                .requestRecord(requestRecord)
                .build();
    }


}
